/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Drawing.Design;
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using ELE.EnterpriseLibrary.Common.Configuration.Design;
using ELE.EnterpriseLibrary.Data.Configuration.Design.Properties;

namespace ELE.EnterpriseLibrary.Data.Configuration.Design
{

  [Image(typeof(ObjectTypeDefNode))]
  [SelectedImage(typeof(ObjectTypeDefNode))]
  public class ObjectTypeDefNode : ConfigurationNode, IAssemblyReferencingObject
  {
    private Type type;
    private ObjectTypeAssemblyNode assemblyNode;
    private EventHandler<ConfigurationNodeChangedEventArgs> assemblyNodeRemovedHandler;

    public ObjectTypeDefNode()
	    : this(new TypeData(Resource.DefaultObjectTypeDefName))
    {
        
    }

    public ObjectTypeDefNode(TypeData data)
      : base((data == null) ? Resource.DefaultObjectTypeDefName : data.Name)
    {
      if (data == null) { throw new ArgumentNullException("TypeData"); }
      this.assemblyNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnAssemblyNodeRemoved);
    }		

    [Browsable(false)]
    public TypeData TypeData
    {
      get
      {
        TypeData data = new TypeData(Name);
        data.Assembly = this.assemblyNode.AssemblyData.Name;
        data.Type = this.type.FullName;
	      return data;
      }
    }

    [Required]
    [SRDescription("ObjectTypeDefNodeTypeDescription", typeof(Resource))]
    [Editor(typeof(AssemblyTypeSelectorEditor), typeof(UITypeEditor))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public Type Type
    {
      get { return type; }
      set { type = value; }
    }

    [Required]
    [SRDescription("ObjectTypeAssemblyReferenceDescription", typeof(Resource))]
    [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
    [ReferenceType(typeof(ObjectTypeAssemblyNode))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public ObjectTypeAssemblyNode Assembly
    {
      get { return assemblyNode; }
      set
      {
        assemblyNode = LinkNodeHelper.CreateReference<ObjectTypeAssemblyNode>(assemblyNode,
                                                                    value,
                                                                    assemblyNodeRemovedHandler,
                                                                    null);
      }
    }
    protected override void Dispose(bool disposing)
    {
      try
      {
        if (disposing)
        {
          if (null != assemblyNode)
          {
            assemblyNode.Removed -= assemblyNodeRemovedHandler;
          }
        }
      }
      finally
      {
        base.Dispose(disposing);
      }
    }

    private void OnAssemblyNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
    {
      this.assemblyNode = null;
    }

    #region IAssemblyReferencingObject Members

    public Assembly GetReferencedAssembly()
    {
      return this.Assembly.Assembly;
    }

    public string GetReferencedAssemblyName()
    {
      return this.Assembly.Assembly.FullName;
    }
    #endregion
  }
}